local skel = fk.CreateSkill {
  name = "emo__zhengnan",
}

Fk:loadTranslationTable{
  ["emo__zhengnan"] = "征南",
  [":emo__zhengnan"] = "以下阶段结束时，你可以将一张牌当【杀】使用：准备阶段，且此【杀】伤害-1；摸牌阶段，且底牌须为红色；出牌阶段，且底牌可为本回合受到过伤害的角色场上的牌。",

  ["#emo__zhengnan-start"] = "征南：你可以将1张牌当伤害-1的【杀】使用",
  ["#emo__zhengnan-draw"] = "征南：你可以将1张红色牌当【杀】使用",
  ["#emo__zhengnan-play"] = "征南：你可以1张牌当【杀】使用",
  ["#emo__zhengnan-subcard"] = "征南：你可以此牌当【杀】使用",
  ["#emo__zhengnan-choose"] = "征南：选择1名本回合受到伤害的角色，将其场上1张牌当【杀】使用",

  ["$emo__zhengnan1"] = "一马当先，万险皆破！", -- 准备
  ["$emo__zhengnan2"] = "关氏威名，可敌千军！", -- 摸牌
  ["$emo__zhengnan3"] = "蛮夷之辈，不足为惧！", -- 出牌
}

skel:addEffect(fk.EventPhaseEnd, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      local phase = player.phase
      if phase == Player.Start or phase == Player.Draw then
        return not player:isNude()
      elseif phase == Player.Play then
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local prompt = ""
    local params = { ---@type askToUseVirtualCardParams
      skill_name = skel.name, name = "slash", skip = true,
    }
    if player.phase == Player.Play then
      prompt = "#emo__zhengnan-play"
      local victims = {}
      player.room.logic:getActualDamageEvents(1, function(e)
        local to = e.data.to
        if to and not to.dead and #to:getCardIds("ej") > 0 then
          table.insertIfNeed(victims, to)
        end
      end)
      if #victims > 0 then
        local to = room:askToChoosePlayers(player, {
          prompt = "#emo__zhengnan-choose", min_num = 1, max_num = 1, skill_name = skel.name, targets = victims
        })[1]
        if to then
          local cid = room:askToChooseCard(player, {target = to, flag = "ej", skill_name = skel.name})
          params.subcards = {cid}
          prompt = "#emo__zhengnan-subcard"
        end
      end
    end
    if not params.subcards then
      params.card_filter = { n = {1, 1} }
      if player.phase == Player.Draw then
        params.card_filter.pattern = ".|.|diamond,heart"
        prompt = "#emo__zhengnan-draw"
      elseif player.phase == Player.Start then
        prompt = "#emo__zhengnan-start"
      end
    end
    params.prompt = prompt
    local use = room:askToUseVirtualCard(player, params)
    if not use then return false end
    if player.phase == Player.Start then
      use.additionalDamage = (use.additionalDamage or 0) -1
    end
    event:setCostData(self, {use = use})
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:useCard(event:getCostData(self).use)
  end,
})

return skel
